package com.shuoyue.ycgk.views.dialog.addresspicker;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.entity.address.AddressBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddressPickerDialog extends Dialog {
    @BindView(R.id.recycler_view1)
    RecyclerView recyclerView1;
    @BindView(R.id.recycler_view2)
    RecyclerView recyclerView2;
    @BindView(R.id.recycler_view3)
    RecyclerView recyclerView3;
    @BindView(R.id.selected)
    TextView selected;
    private Context mContext;


    PickerAdapter pickerAdapter1;
    PickerAdapter pickerAdapter2;
    PickerAdapter pickerAdapter3;

    public AddressPickerDialog(@NonNull Context context, int maxLevl) {
        super(context, R.style.dialog);
        this.mContext = context;
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setWindowAnimations(R.style.ChooseStyle);
        View view = View.inflate(context, R.layout.dialog_address, null);
        setContentView(view);
        ButterKnife.bind(this, view);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        getWindow().setGravity(Gravity.BOTTOM);
        initView(maxLevl);
    }

    private void initView(int maxLevl) {
        if (maxLevl == 1) {
            recyclerView1.setVisibility(View.VISIBLE);
        } else if (maxLevl == 2) {
            recyclerView2.setVisibility(View.VISIBLE);
        } else {
            recyclerView3.setVisibility(View.VISIBLE);
        }
        recyclerView1.addItemDecoration(new DividerItemDecoration(mContext, LinearLayout.VERTICAL));
        recyclerView2.addItemDecoration(new DividerItemDecoration(mContext, LinearLayout.VERTICAL));
        recyclerView3.addItemDecoration(new DividerItemDecoration(mContext, LinearLayout.VERTICAL));

        recyclerView1.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView2.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView3.setLayoutManager(new LinearLayoutManager(mContext));
        pickerAdapter1 = new PickerAdapter(null);
        recyclerView1.setAdapter(pickerAdapter1);
        pickerAdapter2 = new PickerAdapter(null);
        recyclerView2.setAdapter(pickerAdapter2);
        pickerAdapter3 = new PickerAdapter(null);
        recyclerView3.setAdapter(pickerAdapter3);


        pickerAdapter1.setOnItemChildClickListener((adapter, view, position) -> {
            AddressBean addressBean = (AddressBean) adapter.getItem(position);
            for (AddressBean addressBean1 : pickerAdapter1.getData()) {
                addressBean1.setSelect(false);
            }
            addressBean.setSelect(true);
            pickerAdapter1.notifyDataSetChanged();
            if (pickerLevel1 != null) {
                pickerLevel1.picked(addressBean);
            }
            selected.setText(addressBean.getName());
        });
        pickerAdapter2.setOnItemChildClickListener((adapter, view, position) -> {
            AddressBean addressBean = (AddressBean) adapter.getItem(position);
            for (AddressBean AddressBean1 : pickerAdapter2.getData()) {
                AddressBean1.setSelect(false);
            }
            addressBean.setSelect(true);
            pickerAdapter2.notifyDataSetChanged();

            if (pickerLevel2 != null) {
                pickerLevel2.picked(pickerAdapter1.getSelect(), addressBean);
            }
            selected.setText(pickerAdapter1.getSelect().getName() + "/" + addressBean.getName());

        });
        pickerAdapter3.setOnItemChildClickListener((adapter, view, position) -> {
            AddressBean addressBean = (AddressBean) adapter.getItem(position);
            for (AddressBean AddressBean1 : pickerAdapter3.getData()) {
                AddressBean1.setSelect(false);
            }
            addressBean.setSelect(true);
            pickerAdapter3.notifyDataSetChanged();

            if (pickerLevel3 != null) {
                pickerLevel3.picked(pickerAdapter1.getSelect(),
                        pickerAdapter2.getSelect(),
                        addressBean);
            }
            selected.setText(pickerAdapter1.getSelect().getName() + "/" + pickerAdapter2.getSelect().getName() + "/" + addressBean.getName());

        });
    }

    @OnClick({R.id.cancer, R.id.submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cancer:
                dismiss();
                break;
            case R.id.submit:
                if (pickerFinishCallback != null) {
                    pickerFinishCallback.picked(pickerAdapter1.getSelect(), pickerAdapter2.getSelect(), pickerAdapter3.getSelect());
                }
                break;
        }
    }

    public void setPickerFinishCallback(PickerFinishCallback pickerFinishCallback) {
        this.pickerFinishCallback = pickerFinishCallback;
    }

    public void setPickerLevel1(PickerLevel1 pickerLevel1) {
        this.pickerLevel1 = pickerLevel1;
    }

    public void setPickerLevel2(PickerLevel2 pickerLevel2) {
        this.pickerLevel2 = pickerLevel2;
    }

    public void setPickerLevel3(PickerLevel3 pickerLevel3) {
        this.pickerLevel3 = pickerLevel3;
    }

    PickerFinishCallback pickerFinishCallback;
    PickerLevel1 pickerLevel1;
    PickerLevel2 pickerLevel2;
    PickerLevel3 pickerLevel3;


    public interface PickerFinishCallback {
        void picked(AddressBean... beans);
    }

    public interface PickerLevel1 {
        void picked(AddressBean beans);
    }

    public interface PickerLevel2 {
        void picked(AddressBean beans1, AddressBean beans2);
    }

    public interface PickerLevel3 {
        void picked(AddressBean beans1, AddressBean beans2, AddressBean beans3);
    }

    public void setLevl1Data(List<AddressBean> AddressBeans) {
        if (recyclerView1.getVisibility() == View.VISIBLE) {
            pickerAdapter1.setNewData((List<AddressBean>) AddressBeans);
        }
    }

    public void setLevl2Data(List<AddressBean> AddressBeans) {
        if (recyclerView2.getVisibility() == View.VISIBLE) {
            pickerAdapter2.setNewData((List<AddressBean>) AddressBeans);
        }
    }

    public void setLevl3Data(List<AddressBean> AddressBeans) {
        if (recyclerView3.getVisibility() == View.VISIBLE) {
            pickerAdapter3.setNewData((List<AddressBean>) AddressBeans);
        }
    }
}
